# This file creates estimates for parents for whom pretreatment co --------

# install.packages("dplyr")
# install.packages("date")
# install.packages("AER")
# install.packages("rio")

library(dplyr)
library(date)
library(AER)
library(rio)

# Load data

load("mothers.rdata")

# load administrative 
udd <- import(paste("udda", 2007, ".dta", sep = ""))
ind <- import(paste("indh", 2007, ".dta", sep = ""))
bef <- import(paste("bef", 2007, ".dta", sep = ""))
ras <- import(paste("ras", 2007, ".dta", sep = ""))
lpr <- import(paste("lpradm",2007,".sas7bdat", sep = ""))

# Filter of if age of oldest is unkwown (select parents )
mothers <-
  mothers %>%
  filter(!is.na(age_oldest)) 

# select parents who had children between 2009 and 2013 
# one month after 2009 to one month before 2013

mothers13 <- 
  mothers %>%
  filter(! is.na(stemte_2009) & !is.na(stemt) & first13 > 0) %>%
  filter(age_oldest >= (14565 + 30) & age_oldest <= (16028 - 30)) %>%
  mutate(vote09 = stemte_2009 == "Stemte",
         vote = stemt == "Stemte",
         no_children = no_children_13)

mothers13 <-
  within(mothers13, {
    age        <- ntile(date, 25)
    twin_first <- twin_first > 0
  })

mothers13 <-
  mothers13 %>%
  group_by(age) %>%
  mutate(child_age = ntile(age_oldest, 2))

# take log of days in hospital
lpr <- 
  lpr %>% 
  filter(!is.na(V_SENGDAGE)) %>% 
  group_by(pnr) %>% 
  summarise(logbeddays = log(sum(V_SENGDAGE)))

# merge and recode
mothers13 <-
  left_join(mothers13, ras, by = "pnr") %>% 
  mutate(SOCSTIL_KODE = if_else(is.na(SOCSTIL_KODE), 500,SOCSTIL_KODE)) %>% 
  group_by(pnr) %>% 
  mutate(SOCSTIL_KODE_min = min(SOCSTIL_KODE)) %>% 
  filter(SOCSTIL_KODE_min ==SOCSTIL_KODE) %>% 
  sample_n(1) %>%
  mutate(full_time =SOCSTIL_KODE < 200) %>%
  left_join(., udd, by = "pnr") %>% 
  mutate(primary_school = hfaudd_hovedgruppe == 10, 
         high_school    = hfaudd_hovedgruppe == 20 | hfaudd_hovedgruppe == 25,
         vocational     = hfaudd_hovedgruppe == 35, 
         short_tertiary = hfaudd_hovedgruppe == 40, 
         bachelor       = hfaudd_hovedgruppe == 50 | hfaudd_hovedgruppe == 60,
         masters_phd    = hfaudd_hovedgruppe == 65 | hfaudd_hovedgruppe == 70) %>%
  left_join(., ind, by = "pnr") %>% 
  mutate(log_income = ifelse(perindkialt > 0, log(perindkialt), 0)) %>%
  left_join(., bef, by = "pnr") %>% 
  mutate(danish = IE_TYPE == 1)  %>%
  ungroup() %>%
  mutate(log_income_mis = is.na(log_income),
         log_income = ifelse(is.na(log_income), mean(log_income, na.rm = TRUE), log_income),
         primary_school = ifelse(is.na(primary_school) , FALSE, primary_school ),
         high_school    = ifelse(is.na(high_school   ) , FALSE, high_school    ),
         vocational     = ifelse(is.na(vocational    ) , FALSE, vocational     ),
         short_tertiary = ifelse(is.na(short_tertiary) , FALSE, short_tertiary ),
         bachelor       = ifelse(is.na(bachelor      ) , FALSE, bachelor       ),
         masters_phd    = ifelse(is.na(masters_phd   ) , FALSE, masters_phd    ),
         danish = ifelse(is.na(danish), FALSE, danish)) %>%
  left_join(., lpr, by = "pnr")  %>% 
  mutate(logbeddays = ifelse(!is.na(logbeddays), logbeddays, 0))


mod0 <- lm(vote ~ twin_first, data = mothers13)
mod1 <-
  lm(vote ~ twin_first + as.factor(age)*as.factor(child_age),
     data = mothers13)

mod2 <-
  lm(vote ~ twin_first + vote09 + as.factor(age)*as.factor(child_age),
     data = mothers13)

mod3 <- lm(vote ~ twin_first + vote09 + as.factor(age) * as.factor(child_age) +
             primary_school + high_school + vocational + short_tertiary +
             bachelor +  masters_phd + log_income + full_time + danish + log_income_mis +
             logbeddays, 
           data = mothers13)

mod1_iv  <- ivreg(vote ~ no_children + as.factor(age) * as.factor(child_age) | 
                    twin_first + as.factor(age) * as.factor(child_age), data = mothers13)

mod2_iv  <- ivreg(vote ~ no_children + vote09 + as.factor(age) * as.factor(child_age) | 
                    twin_first + vote09 + as.factor(age) * as.factor(child_age), data = mothers13)

mod3_iv  <- ivreg(vote ~ no_children + vote09 + as.factor(age) * as.factor(child_age) +
                    primary_school + high_school + vocational + short_tertiary +
                    bachelor +  masters_phd + log_income + full_time + danish  + log_income_mis + logbeddays | 
                    twin_first + vote09 + as.factor(age) * as.factor(child_age) +
                    primary_school + high_school + vocational + short_tertiary +
                    bachelor +  masters_phd + log_income + full_time + danish + log_income_mis + logbeddays, data = mothers13)

table_mom_13 <- rbind(c(summary(mod0)$coefficients[1,], with(mothers13, table(twin_first))),
                      c(summary(mod1)$coefficients[2,], with(mothers13, table(twin_first))),
                      c(summary(mod2)$coefficients[2,], with(mothers13, table(twin_first))),
                      c(summary(mod3)$coefficients[2,], with(mothers13, table(twin_first))),
                      c(summary(mod1_iv)$coefficients[2,], with(mothers13, table(twin_first))),
                      c(summary(mod2_iv)$coefficients[2,], with(mothers13, table(twin_first))),
                      c(summary(mod3_iv)$coefficients[2,], with(mothers13, table(twin_first))))

# Find F diagnostics 
sum1_iv <- summary(mod1_iv, diagnostics = TRUE)
sum2_iv <- summary(mod2_iv, diagnostics = TRUE)
sum3_iv <- summary(mod3_iv, diagnostics = TRUE)

f_diag_mothers <- 
  rbind(sum1_iv$diagnostics[1,],
        sum2_iv$diagnostics[1,],
        sum3_iv$diagnostics[1,])

#Her: hvis det virker, s? gentag for 2014 og rbind til et stort objekt. kopier til f?dre 
## Repeat for 2014

# select parents who had children between 2009 and 2014 
# one month after 2009 to one month before 2014

mothers14 <- 
  mothers %>%
  filter(! is.na(stemte_2009) & !is.na(ep_stemt) & first13 > 0) %>%
  filter(age_oldest >= (14565 + 30) & age_oldest <= (16215 - 30)) %>%
  mutate(vote09 = stemte_2009 == "Stemte",
         vote = ep_stemt,
         no_children = no_children_14)

mothers14 <-
  within(mothers14, {
    age        <- ntile(date, 25)
    twin_first <- twin_first > 0
  })

mothers14 <-
  mothers14 %>%
  group_by(age) %>%
  mutate(child_age = ntile(age_oldest, 2))

mothers14 <-
  left_join(mothers14, ras, by = "pnr") %>% 
  mutate(SOCSTIL_KODE = if_else(is.na(SOCSTIL_KODE), 500,SOCSTIL_KODE)) %>% 
  group_by(pnr) %>% 
  mutate(SOCSTIL_KODE_min = min(SOCSTIL_KODE)) %>% 
  filter(SOCSTIL_KODE_min ==SOCSTIL_KODE) %>% 
  sample_n(1) %>%
  mutate(full_time =SOCSTIL_KODE < 200) %>%
  left_join(., udd, by = "pnr") %>% 
  mutate(primary_school = hfaudd_hovedgruppe == 10, 
         high_school    = hfaudd_hovedgruppe == 20 | hfaudd_hovedgruppe == 25,
         vocational     = hfaudd_hovedgruppe == 35, 
         short_tertiary = hfaudd_hovedgruppe == 40, 
         bachelor       = hfaudd_hovedgruppe == 50 | hfaudd_hovedgruppe == 60,
         masters_phd    = hfaudd_hovedgruppe == 65 | hfaudd_hovedgruppe == 70) %>%
  left_join(., ind, by = "pnr") %>% 
  mutate(log_income = ifelse(perindkialt > 0, log(perindkialt), 0)) %>%
  left_join(., bef, by = "pnr") %>% 
  mutate(danish = IE_TYPE == 1)  %>%
  ungroup() %>%
  mutate(log_income_mis = is.na(log_income),
         log_income = ifelse(is.na(log_income), mean(log_income, na.rm = TRUE), log_income),
         primary_school = ifelse(is.na(primary_school) , FALSE, primary_school ),
         high_school    = ifelse(is.na(high_school   ) , FALSE, high_school    ),
         vocational     = ifelse(is.na(vocational    ) , FALSE, vocational     ),
         short_tertiary = ifelse(is.na(short_tertiary) , FALSE, short_tertiary ),
         bachelor       = ifelse(is.na(bachelor      ) , FALSE, bachelor       ),
         masters_phd    = ifelse(is.na(masters_phd   ) , FALSE, masters_phd    ),
         danish = ifelse(is.na(danish), FALSE, danish)) %>%
  left_join(., lpr, by = "pnr")  %>% 
  mutate(logbeddays = ifelse(!is.na(logbeddays), logbeddays, 0))


mod0 <- lm(vote ~ twin_first, data = mothers14)
mod1 <-
  lm(vote ~ twin_first + as.factor(age)*as.factor(child_age),
     data = mothers14)

mod2 <-
  lm(vote ~ twin_first + vote09 + as.factor(age)*as.factor(child_age),
     data = mothers14)

mod3 <- lm(vote ~ twin_first + vote09 + as.factor(age) * as.factor(child_age) +
             primary_school + high_school + vocational + short_tertiary +
             bachelor +  masters_phd + log_income + full_time + danish + log_income_mis +
             logbeddays, 
           data = mothers14)

mod1_iv  <- ivreg(vote ~ no_children + as.factor(age) * as.factor(child_age) | 
                    twin_first + as.factor(age) * as.factor(child_age), data = mothers14)

mod2_iv  <- ivreg(vote ~ no_children + vote09 + as.factor(age) * as.factor(child_age) | 
                    twin_first + vote09 + as.factor(age) * as.factor(child_age), data = mothers14)

mod3_iv  <- ivreg(vote ~ no_children + vote09 + as.factor(age) * as.factor(child_age) +
                    primary_school + high_school + vocational + short_tertiary +
                    bachelor +  masters_phd + log_income + full_time + danish  + log_income_mis + logbeddays | 
                    twin_first + vote09 + as.factor(age) * as.factor(child_age) +
                    primary_school + high_school + vocational + short_tertiary +
                    bachelor +  masters_phd + log_income + full_time + danish + log_income_mis + logbeddays, data = mothers14)

table_mom_14 <- rbind(c(summary(mod0)$coefficients[1,], with(mothers14, table(twin_first))),
                      c(summary(mod1)$coefficients[2,], with(mothers14, table(twin_first))),
                      c(summary(mod2)$coefficients[2,], with(mothers14, table(twin_first))),
                      c(summary(mod3)$coefficients[2,], with(mothers14, table(twin_first))),
                      c(summary(mod1_iv)$coefficients[2,], with(mothers14, table(twin_first))),
                      c(summary(mod2_iv)$coefficients[2,], with(mothers14, table(twin_first))),
                      c(summary(mod3_iv)$coefficients[2,], with(mothers14, table(twin_first))))

table_mom_out <- 
  rbind(table_mom_13, table_mom_14)

# Find F diagnostics 
sum1_iv <- summary(mod1_iv, diagnostics = TRUE)
sum2_iv <- summary(mod2_iv, diagnostics = TRUE)
sum3_iv <- summary(mod3_iv, diagnostics = TRUE)

f_diag_mothers <- 
  rbind(f_diag_mothers,
        sum1_iv$diagnostics[1,],
        sum2_iv$diagnostics[1,],
        sum3_iv$diagnostics[1,])

write.table(table_mom_out, file = "mother_post09.txt")

write.table(f_diag_mothers, file = "f_stat_mothers_post09.txt")

